package com.easylinkin.linkappapi.webcammanage.util;

import com.easylinkin.linkappapi.common.utils.DateUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * class info :
 *
 * @author 刘启航
 * @date 2021/4/14 17:36
 */
public class YYSHDateUtil {

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String dateRegex = "^((\\d{2}(([02468][048])|([13579][26]))\\-((((0?[13578])|(1[02]))\\-((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))\\-((0?[1-9])|([1-2][0-9])|(30)))|(0?2\\-((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))\\-((((0?[13578])|(1[02]))\\-((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))\\-((0?[1-9])|([1-2][0-9])|(30)))|(0?2\\-((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s((([0-1][0-9])|(2?[0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))$";

    /**
     *  正浮点正则
     */
    public static String zhengFloatNumRegex = "(0|[1-9]\\\\d*)(\\\\.\\\\d+)?";
    /**
     * 负浮点正则
     */
    public static String fuFloatNumRegex = "-?(0|[1-9]\\\\d*)(\\\\.\\\\d+)?";


    /**
     * 获取本周开始和结束日期
     * @return
     */
    public static Date[] getCurrentWeekStartAndEndDate(){
        Date[] dates = new Date[2];
        LocalDate today = LocalDate.now();
        LocalDate monday = today.with(TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY));
        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY ));
        dates[0] = localDateToDate(monday);
        dates[1] = localDateToDate(sunday);
        return dates;
    }

    /**
     * 获取上一周周开始和结束日期
     * @return
     */
    public static Date[] getBeforeWeekStartAndEndDate(){
        Date[] dates = new Date[2];
        LocalDate today = LocalDate.now();
        LocalDate monday = today.with(TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY));
//        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY ));
        Date currentStartDate = localDateToDate(monday);
        Date bSunday = DateUtil.addDay(currentStartDate, -1);
        Date bMonday = DateUtil.addDay(currentStartDate, -7);
        dates[0] = bMonday;
        dates[1] = bSunday;
        return dates;
    }

    /**
     * localDate转换为Date
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取当前月第一天
     * @return
     */
    public static String getFirstDayOfMonth() {
        SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT_DAY);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH,1);
        String firstDay = format.format(c.getTime());
        return firstDay;
    }

    /**
     * 获取一年内所有月数据
     * @param year
     * @return
     */
    public static List<String> getMonthByYear(String year){
        List<String> list = new ArrayList<String>();
        list.add(year + "-01");
        list.add(year + "-02");
        list.add(year + "-03");
        list.add(year + "-04");
        list.add(year + "-05");
        list.add(year + "-06");
        list.add(year + "-07");
        list.add(year + "-08");
        list.add(year + "-09");
        list.add(year + "-10");
        list.add(year + "-11");
        list.add(year + "-12");
        return list;
    }

    /**
     * 获取一天内所有小时段的数据
     * @param currentDay
     * @return
     */
    public static List<String> getHourByDay(String currentDay){
        List<String> list = new ArrayList<String>();
        list.add(currentDay +" 00");
        list.add(currentDay +" 01");
        list.add(currentDay +" 02");
        list.add(currentDay +" 03");
        list.add(currentDay +" 04");
        list.add(currentDay +" 05");
        list.add(currentDay +" 06");
        list.add(currentDay +" 07");
        list.add(currentDay +" 08");
        list.add(currentDay +" 09");
        list.add(currentDay +" 10");
        list.add(currentDay +" 11");
        list.add(currentDay +" 12");
        list.add(currentDay +" 13");
        list.add(currentDay +" 14");
        list.add(currentDay +" 15");
        list.add(currentDay +" 16");
        list.add(currentDay +" 17");
        list.add(currentDay +" 18");
        list.add(currentDay +" 19");
        list.add(currentDay +" 20");
        list.add(currentDay +" 21");
        list.add(currentDay +" 22");
        list.add(currentDay +" 23");
        return list;
    }

    /**
     * 获取指定年月内所有的天数据
     * @param yearParam
     * @param monthParam
     * @return
     */
    public static List<String> getDayByMonth(int yearParam, int monthParam){
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }

    /**
     * 获取时间段内所有的月数据
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getMonths(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.MONTH, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.MONTH, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 获取时间段内所有的天数据
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    public static void main(String[] args) {
        System.out.println(getHourByDay(DateUtil.getCurrentDateStr(DateUtil.DATE_TIME_FORMAT_DAY)));
//        String indices = null;
//        List<String> months = getMonths("2021-09-15 00:00:00", "2021-09-15 23:59:59");
//        StringBuilder sb = new StringBuilder();
//        months.forEach(month -> {
//            sb.append(",").append("deviceflow-").append(month.replaceAll("-", ""));
//        });
//        String s = sb.toString();
//        indices = s.substring(1);
//        System.out.println(indices);
    }


    public static boolean checkDateFormat(String dateStr){
        if (dateStr.matches(dateRegex)) {
            return true;
        }
        return false;
    }

    //正则匹配  如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
    public static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static Long getDateInteval(String startTime, String endTime, String dateFormat){
//        SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT_DEFAULT);
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        //一天的毫秒数
        long nd = 1000*60*60*24;
        //一小时的毫秒数
        long nh = 1000*60*60;
        //一分钟的毫秒数
        long nm = 1000*60;
        //一秒钟的毫秒数
        long ns = 1000;

        long diff = 0;
        try{
            //获取两个时间的毫秒时间差
            diff = format.parse(endTime).getTime() - format.parse(startTime).getTime();
        }catch (Exception e){
            e.printStackTrace();
        }

        long day = diff/nd;//计算相差的天数

        long hour = diff%nd/nh;//计算相差的小时
//        System.err.println("时间相差："+day+"天"+hour+"小时");
        return day*24 + hour;
    }

}
